package ${cfg.basePackage}.controller;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import com.rdkl.printing.config.ShiroUtils;
import ${cfg.basePackage}.model.${entity};
import com.rdkl.printing.model.User;
import ${cfg.basePackage}.service.${table.serviceName};

import lombok.extern.slf4j.Slf4j;


/**
 * ${table.comment} Controller
 *
 * @author ${author}
 * @date ${date}
 */
@Slf4j
@Controller
@RequestMapping("${entity?uncap_first}")
public class ${entity}Controller {

    @Autowired
    private ${entity}Service ${table.serviceName?uncap_first};

    /**
     * 列表页面
     *
     * @author ${author}
     * @date ${date}
     */
    @GetMapping("list")
    public String list(){
        return "${cfg.moduleName}/${entity?uncap_first}_list";
    }

    /**
    * 获取${table.comment}列表数据
    *
    * @Author ${author}
    * @Date ${date}
    */
    @ResponseBody
    @PostMapping(value = "page${entity}Data", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String page${entity}Data(@RequestBody Map<String, Object> paramMap) {
        JSONObject result = new JSONObject();
        try {
            User user = ShiroUtils.getCurrentUser();
            paramMap.put("pressId", user.getPressId());
            PageInfo<${entity}DTO> page = ${table.serviceName?uncap_first}.getPage${entity}(paramMap);
            result.put("rows", page.getList());
            result.put("total", page.getTotal());
        } catch (Exception e) {
            log.error("获取${table.comment}列表数据失败", e);
        }
        return JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
    }

    /**
    * 添加修改页面
    *
    * @author ${author}
    * @date ${date}
    */
    @GetMapping("form")
    public String form(Integer ${cfg.keyFieldHump}, Model model){
        if (${cfg.keyFieldHump} != null){
            ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.get${entity}ById(${cfg.keyFieldHump});
            model.addAttribute("${entity?uncap_first}", ${entity?uncap_first});
        }
        return "${cfg.moduleName}/${entity?uncap_first}_form";
    }

    /**
     * 保存更新${table.comment}信息
     *
     * @author ${author}
     * @date ${date}
     */
    @ResponseBody
    @PostMapping(value = "save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(${entity} ${entity?uncap_first}){
        JSONObject result = new JSONObject();
        try {
            User user = ShiroUtils.getCurrentUser();
            if(${entity?uncap_first}.get${cfg.keyFieldHump?cap_first}() == null){
                ${entity?uncap_first}.setPressId(user.getPressId());
                ${entity?uncap_first}.setCreator(user.getUserId());
                ${entity?uncap_first}.setCreateTime(new Date());
                ${table.serviceName?uncap_first}.insert${entity}(${entity?uncap_first});
            }else{
                ${entity?uncap_first}.setLastEditor(user.getUserId());
                ${entity?uncap_first}.setLastEditTime(new Date());
                ${table.serviceName?uncap_first}.update${entity}(${entity?uncap_first});
            }
            result.put("flag", true);
        } catch (Exception e) {
            result.put("flag", false);
            log.error("保存${table.comment}信息失败", e);
        }
        return JSON.toJSONString(result);
    }

    /**
     * 删除${table.comment}数据
     *
    * @Author ${author}
    * @Date ${date}
     */
    @ResponseBody
    @PostMapping(value = "delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestParam(value = "ids") List<Integer> ids) {
        JSONObject result = new JSONObject();
        try {
            ${table.serviceName?uncap_first}.delete${entity}ByIds(ids);
            result.put("flag", true);
        } catch (Exception e) {
            result.put("flag", false);
            result.put("msg", "删除${table.comment}数据异常");
            log.error("删除${table.comment}数据异常", e);
        }
        return JSON.toJSONString(result);
    }

     /**
      * 详情查看
      *
      * @author ${author}
      * @param ${cfg.keyFieldHump}
      * @date ${date}
      */
      @GetMapping("show")
      public String show(Integer ${cfg.keyFieldHump}, Model model){
        if (${cfg.keyFieldHump} != null){
          ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.get${entity}ById(${cfg.keyFieldHump});
          model.addAttribute("${entity?uncap_first}", ${entity?uncap_first});
        }
        return "${cfg.moduleName}/${entity?uncap_first}_show";
      }

}